package com.songshuai.sales.client.controller;

import com.songshuai.sales.client.feign.PanguApiClient;
import com.songshuai.sales.client.service.SalesInfoService;
import com.songshuai.webapi.baseapi.constant.miniprogram.CacheKeys;
import com.songshuai.webapi.baseapi.model.sales.SalesInfo;
import com.songshuai.webapi.common.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
@Slf4j
public class BaseController {

    @Autowired
    public RedisUtil redisUtil;

    @Autowired
    public PanguApiClient panguApiClient;

    @Autowired
    public SalesInfoService salesInfoService;

    @Autowired
     public HttpServletRequest request;

   public String getCurrentToken(){
        Object token = request.getHeader("token");
        if(null!=token){
            log.info("当前token:"+String.valueOf(token));
            return (String)token;
        }else{
            return null;
        }
    }

    public String getCurrentUserId(){
        String token = this.getCurrentToken();
        if(StringUtils.isNotBlank(token)){
            Object o =  redisUtil.hget(token, CacheKeys.PANGU_USER_ID);
            if (null != o) {
                return (String) o;
            } else {
                SalesInfo salesInfo = salesInfoService.querySalesInfoByToken(token);
                if (null != salesInfo) {
                    return salesInfo.getUserId();
                } else {
                    return null;
                }
            }
        }else{
            return null;
        }
    }
    public String getCurrentUserOpenId(){
        String token = this.getCurrentToken();
        if (StringUtils.isNotBlank(token)) {
            Object o = redisUtil.hget(token, CacheKeys.OPENID);
            if (null != o) {
                return (String) o;
            } else {
                SalesInfo salesInfo = salesInfoService.querySalesInfoByToken(token);
                if (null != salesInfo) {
                    return salesInfo.getOpenId();
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    }




/*    public String getAccessTokenByAppId(String appId){
        String accessToken = (String)redisUtil.get(appId);
        if(StringUtils.isBlank(accessToken)) {
            //String wxspAppid = "wxb2cb3caa4ff29b27";
            // 小程序的 app secret (在微信小程序管理后台获取)
            String wxspSecret = "f5c96e1d1241463746731b651829c045";
            //这里直接写死就可以，不用改，用法可以去看api
            String grant_type = "client_credential";
            //封装请求数据
            String params = "grant_type=" + grant_type + "&secret=" + wxspSecret + "&appid=" + appId;
            //发送GET请求
            String sendGet = HttpClientUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(sendGet);
            //拿到accesstoken
            if (ResponseStatus.ACCESS_TOKEN_ERRCODE.getErrorCode() == json.getInteger("errcode")) {
                throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE);
            }
            if (ResponseStatus.ACCESS_TOKEN_ERRCODE_40001.getErrorCode() == json.getInteger("errcode")) {
                throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40001);
            }
            if (ResponseStatus.ACCESS_TOKEN_ERRCODE_40002.getErrorCode() == json.getInteger("errcode")) {
                throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40002);
            }
            if (ResponseStatus.ACCESS_TOKEN_ERRCODE_40013.getErrorCode() == json.getInteger("errcode")) {
                throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40013);
            }
            accessToken = json.getString("access_token");
            redisUtil.set(appId, accessToken, 7200);
        }
        return accessToken;
    }*/

}
